package br.ufu.si.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import br.ufu.si.bo.GenericBO;
import br.ufu.si.model.base.EstadosCrud;

public abstract class Crud extends AbstractBeanField{

	private JFrame frame;
	private JButton btnNovo;
	private JButton btnCancel;
	private JButton btnSalvar;
	private JButton btnDelete;
	private JButton btnEditar;
	private Principal principal;
	protected JPanel panelForm;
	private JPanel panelFormActions;
	private JButton btnPesquisar;
	protected GenericBO bo;
	private JScrollPane scrollPane;
	protected JTable tableGrid;
	protected List<?> listBean;
	protected DefaultTableModel tableModel;
	
	/**
	 * Create the application.
	 */
	public Crud(Principal principal) {
		super();
		
		this.principal = principal; 
		bo = new GenericBO();
		
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	protected void initialize() {
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				fecharJanela();
			}
		});
		frame.setBounds(100, 100, 740, 495);
		
		panelForm = new JPanel();
		panelForm.setBorder(new LineBorder(new Color(0, 0, 0)));

		panelFormActions = new JPanel();
		panelFormActions.setBorder(new LineBorder(new Color(0, 0, 0)));

		btnNovo = new JButton("Novo");
		btnNovo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				novo();
			}
		});
		btnNovo.setIcon(new ImageIcon(CadastroUniforme.class.getResource("/br/ufu/si/view/images/crud/new.png")));
		panelFormActions.add(btnNovo);

		btnEditar = new JButton("Editar");
		btnEditar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editar();
			}
		});
		btnEditar.setIcon(new ImageIcon(CadastroUniforme.class.getResource("/br/ufu/si/view/images/crud/edit.png")));
		panelFormActions.add(btnEditar);

		btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				salvar();
			}
		});
		btnSalvar.setIcon(new ImageIcon(CadastroUniforme.class.getResource("/br/ufu/si/view/images/crud/save.png")));
		panelFormActions.add(btnSalvar);

		btnDelete = new JButton("Deletar");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deletar();
			}
		});
		
		btnPesquisar = new JButton("Pesquisar");
		btnPesquisar.setIcon(new ImageIcon(Crud.class.getResource("/br/ufu/si/view/images/crud/find.png")));
		panelFormActions.add(btnPesquisar);
		btnDelete.setIcon(new ImageIcon(CadastroUniforme.class.getResource("/br/ufu/si/view/images/crud/remove.png")));
		panelFormActions.add(btnDelete);

		btnCancel = new JButton("Cancelar");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cancelar();
			}
		});
		btnCancel.setIcon(new ImageIcon(CadastroUniforme.class.getResource("/br/ufu/si/view/images/crud/cancel.png")));
		panelFormActions.add(btnCancel);
		
		scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelForm, GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
						.addComponent(panelFormActions, GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE))
					.addGap(10))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 703, Short.MAX_VALUE)
					.addGap(11))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(11)
					.addComponent(panelForm, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
					.addGap(11)
					.addComponent(panelFormActions, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		tableGrid = new JTable();
		tableGrid.setFillsViewportHeight(true);
		scrollPane.setViewportView(tableGrid);
		frame.getContentPane().setLayout(groupLayout);
	}

	public JFrame getFrame(){
		return frame;
	}
	
	public void fecharJanela(){
		principal.getFrame().setVisible(true);
	}

	public void setGridModel(DefaultTableModel tableModel){
		this.tableModel = tableModel;
		tableGrid.setModel(tableModel);
	}
	
	public void iniciarCrud(){
		
		listBean = bo.findAll(bean.getClass());
		
		if(listBean.size() == 0){
			mudarEstado(EstadosCrud.Pesquisa);
			JOptionPane.showMessageDialog(frame, "Nenhum registro encontrado!");
			return;
		}
		
		popularGrid();
	}
	
	public abstract void popularGrid();

	public void mudarEstado(EstadosCrud estado){
		Component[] componentsForm = panelForm.getComponents();
		Component[] botoesCrud = panelFormActions.getComponents();
		
		ComponentUtil.habilitarComponentes(botoesCrud, false);
		ComponentUtil.limparComponentes(componentsForm);
		
		switch (estado) {
		case Novo:
			
			ComponentUtil.habilitarComponentes(new Component[]{btnSalvar, btnCancel}, true);
			
			break;
			
		case Edicao:
			
			ComponentUtil.habilitarComponentes(new Component[]{btnSalvar, btnCancel}, true);
			
			break;
			
		case Pesquisa:
			
			ComponentUtil.habilitarComponentes(new Component[]{btnNovo, btnPesquisar}, true);
			
			break;
			
		case Visualizacao:
			
			ComponentUtil.habilitarComponentes(new Component[]{btnEditar, btnDelete, btnCancel}, true);
			
			break;

		default:
			break;
		}
	}
	
	public void novo(){
		mudarEstado(EstadosCrud.Novo);
	}
	
	public void salvar(){
		Object bean = getBean();
		return;
		
	}
	
	public void deletar(){
		
	}
	
	public void editar(){
	}
	
	public void cancelar(){
		mudarEstado(EstadosCrud.Pesquisa);
	}
	public JPanel getPanelFormActions() {
		return panelFormActions;
	}
	public JButton getBtnPesquisar() {
		return btnPesquisar;
	}
	public JTable getTableGrid() {
		return tableGrid;
	}
}
